<template>
	<div ref="xsReportChart" v-resize="resizeChart" style="width: 100%;height: 100%;"></div>
</template>

<script>

    import * as echarts from 'echarts';
    import { color, borderColor, legendTextStyle } from './chart-conf';
    import domResize from '../../../../../mixins/dom-resize';
    import { formatMonthDate } from '../../../../../utils/jk/jk-tool';
    export default {
        name: 'XsReportChart',
        props: {
            chartData: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        mixins: [domResize],
        data() {
            return {
                example: null,
                borderColor: '#c5c8ce',
                option: {
                    title: {
                        text: '',
                        textStyle: {
                            fontWeight: 'none'
                        }
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    grid: {
                        top: '30px',
                        right: '10px',
                        bottom: '10px',
                        left: '20px',
                        containLabel: true
                    },
                    legend: {
                        data: ['产量(kg)', '能耗(kw·h)'],
                        textStyle: {
                            color: legendTextStyle
                        }
                    },
                    xAxis: {
                        type: 'category',
                        data: [323, 242, 355, 276, 118, 434, 251],
                        axisLabel: {
                            show: true,
                            color: color

                        },
                        nameTextStyle: {
                            color: 'red'
                        },
                        boundaryGap: true,
                        axisLine: {
                            lineStyle: {
                                color: borderColor
                            }
                        }
                    },
                    yAxis: [
                        {
                            type: 'value',
                            splitLine: {
                                lineStyle: {
                                    color: 'none'
                                }
                            },
                            axisLabel: {
                                show: true,
                                color: color

                            },
                            axisLine: {
                                lineStyle: {
                                    color: borderColor
                                }
                            }
                        },
                        {
                            type: 'value',
                            splitLine: {
                                lineStyle: {
                                    color: 'none'
                                }
                            },
                            axisLabel: {
                                show: true,
                                color: color

                            },
                            axisLine: {
                                lineStyle: {
                                    color: borderColor
                                }
                            }
                        }
                    ],
                    series: [
                        {
                            name: '能耗(kw·h)',
                            type: 'line',
                            data: [],
                            smooth: false,
                            barGap: 0,
                            symbol: 'none',
                            itemStyle: {
                                color: '#2378f7'
                            },
                            areaStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: '#188df0'
                                    },
                                    {
                                        offset: 1,
                                        color: '#2378f7'
                                    }
                                ])
                            }
                        },
                        {
                            name: '产量(kg)',
                            type: 'line',
                            data: [],
                            smooth: false,
                            yAxisIndex: 1,
                            barGap: 0,
                            symbol: 'none',
                            itemStyle: {
                                color: 'rgb(255, 70, 131)'
                            },
                            areaStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: 'rgb(255, 158, 68)'
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgb(255, 70, 131)'
                                    }
                                ])
                            }
                        }
                    ]
                }
            };
        },
        methods: {
            resize() {
                window.addEventListener('resize', () => {
                    this.resizeChart();
                });
            },
            initChart() {
                this.$nextTick(() => {
                    this.example = echarts.init(this.$refs.xsReportChart);
                    this.example.setOption(this.option);
                });
            },
            updateChart() {
                if (this.example) {
                    this.$nextTick(() => {
                        const cpData = this.chartData ? JSON.parse(JSON.stringify(this.chartData)) : [];
                        cpData.forEach(x => {
                            x.belongDate = formatMonthDate(x.belongDate, '/', false);
                        });
                        this.option.xAxis.data = cpData.map(x => x.belongDate);
                        this.option.series[0].data = cpData.map(x => x.shiftElectricEnergy);
                        this.option.series[1].data = cpData.map(x => x.shiftQty);
                        this.example.setOption(this.option);
                    });
                } else {
                    this.initChart();
                }
            }
        },
        mounted() {
            this.resize();
            this.initChart();
        },
        watch: {
            chartData: {
                handler() {
                    this.updateChart();
                },
                deep: true,
                immediate: true
            }
        }
    };
</script>

<style scoped lang="scss"></style>
